<?php
// +----------------------------------------------------------------------
// | 磁盘服务层
// +----------------------------------------------------------------------
// | 主要服务于磁盘路径配置
// +----------------------------------------------------------------------
// | 错误编码头 203xxx
// +----------------------------------------------------------------------
namespace common\service;

use common\model\DiskModel;
use support\ErrorCode;

class DiskService
{

    /**
     * 添加磁盘
     * @param $param
     * @return array
     */
    public function addDisks($param)
    {
        $diskModel = model(DiskModel::class);
        $resData = $diskModel->addItem($param);
        if (!$resData) {
            // 磁盘创建失败错误码 001
            throw_strack_exception($diskModel->getError(), ErrorCode::DISK_CREATE_FAILURE);
        } else {
            // 返回成功数据
            return success_response($diskModel->getSuccessMessage(), $resData);
        }
    }

    /**、
     * 修改磁盘
     * @param $param
     * @return array
     */
    public function modifyDisks($param)
    {
        $diskModel = model(DiskModel::class);
        $resData = $diskModel->modifyItem($param);
        if (!$resData) {
            // 磁盘修改失败错误码 002
            throw_strack_exception($diskModel->getError(), ErrorCode::DISK_UPDATE_FAILURE);
        } else {
            // 返回成功数据
            return success_response($diskModel->getSuccessMessage(), $resData);
        }
    }

    /**
     * 删除磁盘
     * @param $param
     * @return array
     */
    public function deleteDisks($param)
    {
        $diskModel = model(DiskModel::class);
        $resData = $diskModel->deleteItem($param);
        if (!$resData) {
            // 磁盘删除失败错误码 003
            throw_strack_exception($diskModel->getError(), ErrorCode::DISK_DELETE_FAILURE);
        } else {
            // 返回成功数据
            return success_response($diskModel->getSuccessMessage(), $resData);
        }
    }

    /**
     * 获取磁盘Combobox列表
     * @return mixed
     */
    public function getDiskCombobox()
    {
        $diskModel = model(DiskModel::class);
        $diskList = $diskModel->field("id,name")->select();
        return $diskList;
    }

    /**
     * 加载项目存储路径列表
     * @param $param
     * @return array
     */
    public function getDisksGridData($param)
    {

        $options = [
            'page' => [$param["page"], $param["rows"]]
        ];

        $diskModel = model(DiskModel::class);
        $diskData = $diskModel->selectData($options);
        return $diskData;
    }

    /**
     * 通过code返回disk信息
     * @param $code
     * @return array|mixed
     */
    public function getDiskByCode($code)
    {
        $diskModel = model(DiskModel::class);
        $ret = $diskModel->findData(["filter" => ["code" => $code]]);
        return $ret;
    }

    /**
     * 获取云盘配置数据
     * @return array|mixed
     */
    public function getCloudDiskConfig()
    {
        $optionsService = OptionsService::getInstance();
        $couldDiskSettings = $optionsService->getOptionsData("cloud_disk_settings");
        if (!empty($couldDiskSettings) && $couldDiskSettings["open_cloud_disk"] == 1) {
            return $couldDiskSettings;
        }
        return ["open_cloud_disk" => 0];
    }
}
